package com.example.thread.base.demo2;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * @author LiYuhang
 * @version 0.1
 * @application
 * @Date 2020/8/14 10:53
 */
@Slf4j
public class ScheduledThreadPoolExecutor {

    /**
     * 创建一个定长线程池，支持定时及周期性任务执行。延迟执行
     *
     * @param limit
     */
    public static void scheduledThreadPoolExecutor(Integer limit) {
        ScheduledExecutorService executorService = Executors.newScheduledThreadPool(5);

//        Runnable runnable1 = () -> log.info("线程名称：{}，执行3秒后执行",Thread.currentThread().getName());
//        executorService.schedule(runnable1,3,TimeUnit.SECONDS);
        Runnable runnable1_1 = () -> log.info("线程名称：{}，执行5- 10秒后执行",Thread.currentThread().getName());
        executorService.scheduleWithFixedDelay(runnable1_1,5,10,TimeUnit.SECONDS);
        log.info("runnable1_1 任务创建成功");
//        Runnable runnable2 = () -> log.info("线程名称：{}，执行2秒后执行",Thread.currentThread().getName());
//        executorService.schedule(runnable2,2,TimeUnit.SECONDS);
//        Runnable runnable3 = () -> log.info("线程名称：{}，执行普通任务",Thread.currentThread().getName());
//        for (int i = 0; i < limit; i++) {
//            executorService.execute(runnable3);
//        }
//        executorService.shutdown();
    }
}
